$(function()
{
    function get_country()
    {
        $.ajax({
            url: ajax_request_domain + '/base/customs_country_list',
            type: 'post',
            dataType: 'json',
            beforeSend: (xhr) => {
                xhr.setRequestHeader("token", getLocalStorage('token'));
            },
            success: function (res) {
                if (res.code === 1) {
                    let html = '';
                    $.each(res.data, function (index, item) {
                        html += '<li data-value="'+ item.id + '"><span value="' + item.id + '">' + item.name + '</span></li>';
                    });
                    $('#trade_form .higer_box ul[name=importer_country]').html(html);
                    $('#trade_form .higer_box ul[name=exporter_country]').html(html);
                }
                if (res.code === 0)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close(result.msg, 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    })
                }
            },
            error: function (result) {
                if (result.status === 500)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close('System error, please try again later', 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    })
                    return false;
                }
                if (result.status === 401)
                {
                    themes_obj.loginBox('index_type');
                    return false
                }
                else if (result.status  === 403)
                {
                    themes_obj.upgradeBox('index_type');
                    return false;
                }
            }
        });
    }
    get_country();
    var rows = [];
    function trade_list_fun(reload = 0, reloadtimes = 0)
    {
        var $this = $("#trade_form");
        var serialize = $this.serialize();
        // 判断是否需要重新加载
        if (reload == 1)
        {
            $('.data_main .bill .bottom .table tbody').html('');
            $('.data_main .bill .bottom .load_out').show();
            $('#turn_page').html('');
        }
        $.ajax({
            url: ajax_request_domain + '/trade/get_customs_record_list',
            type: 'post',
            dataType: 'json',
            data: serialize,
            beforeSend: (xhr) => {
                xhr.setRequestHeader("token", getLocalStorage('token'));
            },
            success: function(result)
            {
                if (result.code === 1)
                {
                    let ret = result.data;
                    $('.data_main .bill .title_box .abs_top .l span').html(ret.total);
                    let KeyWord = $('.advanced_search_box .input_box .keyword_input').val();
                    let KeyWordType = $('.advanced_search_box .input_box .keyword_input').attr('name');
                    let products = '';
                    let hsCode = '';
                    let Role = $('#Role').val();
                    let result_msg = ret.list;
                    let new_html = '';
                    let this_size = 0;
                    for (k in result_msg)
                    {
                        rows.push(result_msg[k]);
                        for (kk in result_msg[k])
                        {
                            if (result_msg[k][kk] == null || result_msg[k][kk] == undefined || result_msg[k][kk] == 'N/A' || result_msg[k][kk] == 'N-A')
                            {
                                result_msg[k][kk] = lang_obj.trade.detail.un_published
                            }
                        }
                        this_size++;
                        new_html += '<tr' + ((this_size > zuoooo_config.Permit.limits['trade_detail'] && zuoooo_config.Permit.limits['trade_detail']) ? ' class="no_view"' : '') + '>';
                        new_html += '	<td class="normal"><div class="selector"><input type="checkbox" name="select" value="' + result_msg[k]['snow_flake_id'] + '"></div></td>';
                        new_html += '	<td class="normal acctime">' + result_msg[k]['date_text'] + '</td>';
                        new_html += '	<td><span title="Corporate" class="corporate">' + (result_msg[k]['buyer']['name'] == lang_obj.trade.detail.un_published ? result_msg[k]['buyer']['name'] : '<a href="' + result_msg[k]['buyer_url'] + '" target="_blank">' + result_msg[k]['buyer']['name'] + '</a>') + '</span><br />' + result_msg[k]['importer_country']['name'] + '</td>';
                        new_html += '	<td><span title="TradeCorporate" class="trade_corporate">' + (result_msg[k]['seller']['name'] == lang_obj.trade.detail.un_published ? result_msg[k]['seller']['name'] : '<a href="' + result_msg[k]['supplier_url'] + '" target="_blank">' + result_msg[k]['seller']['name'] + '</a>') + '</span><br />' + result_msg[k]['exporter_country']['name'] + '</td>';
                        products = result_msg[k]['product_desc'];
                        hsCode = result_msg[k]['hs_code'];
                        for (k1 in ret['request']['Keywords'])
                        {
                            products = products.replaceAll(ret['request']['Keywords'][k1], '<em>' + ret['request']['Keywords'][k1] + '</em>');
                            hsCode = hsCode.replaceAll(ret['request']['Keywords'][k1], '<em>' + ret['request']['Keywords'][k1] + '</em>')
                        }
                        new_html += '	<td>' + (hsCode ? hsCode : lang_obj.trade.detail.un_published) + '</td>';
                        new_html += '	<td class="sec_value">' + (products ? products : lang_obj.trade.detail.un_published) + '</td>';
                        new_html += '	<td class="nowrap">' + (result_msg[k]['weight'] > 0 ? result_msg[k]['weight'] : lang_obj.trade.detail.un_published) + '</td>';
                        new_html += '	<td><a href="javascript:;" class="go show-customs-record-detail" lid="' + result_msg[k]['snow_flake_id'] + '" aria-label="go"></a></td>';
                        new_html += '</tr>';
                    }
                    if ((zuoooo_config.Permit.limits['trade_detail'] < this_size && zuoooo_config.Permit.limits['trade_detail']) && this_size > 5 && this_size < 10)
                    {
                        for (var size_a = this_size; size_a < 10; size_a++)
                        {
                            new_html += '<tr class="no_view">';
                            new_html += '	<td class="normal"><div class="selector"><input type="checkbox" name="select" value="' + result_msg[k]['snow_flake_id'] + '"></div></td>';
                            new_html += '	<td class="normal acctime">' + result_msg[k]['date_text'] + '</td>';
                            new_html += '	<td><span title="Corporate" class="corporate">' + (result_msg[k]['buyer']['name'] == lang_obj.trade.detail.un_published ? result_msg[k]['buyer']['name'] : '<a href="' + result_msg[k]['buyer_url'] + '" target="_blank">' + result_msg[k]['buyer']['name'] + '</a>') + '</span><br />' + result_msg[k]['importer_country']['name'] + '</td>';
                            new_html += '	<td><span title="TradeCorporate" class="trade_corporate">' + (result_msg[k]['seller']['name'] == lang_obj.trade.detail.un_published ? result_msg[k]['seller']['name'] : '<a href="' + result_msg[k]['supplier_url'] + '" target="_blank">' + result_msg[k]['seller']['name'] + '</a>') + '</span><br />' + result_msg[k]['exporter_country']['name'] + '</td>';
                            new_html += '	<td>' + (hsCode ? hsCode : lang_obj.trade.detail.un_published) + '</td>';
                            new_html += '	<td class="sec_value">' + (products ? products : lang_obj.trade.detail.un_published) + '</td>';
                            new_html += '	<td class="nowrap">' + (result_msg[k]['weight'] > 0 ? result_msg[k]['weight'] : lang_obj.trade.detail.un_published) + '</td>';
                            new_html += '	<td><a href="javascript:;" class="go show-customs-record-detail" lid="' + result_msg[k]['snow_flake_id'] + '" aria-label="go"></a></td>';
                            new_html += '</tr>'
                        }
                    }
                    if (new_html == '')
                    {
                        let consultant = ret.contact;
                        new_html = '<div class="list_empty">';
                        new_html += '<div class="img">';
                        new_html += '<img src="static/image/list_empty.png">';
                        new_html += '<span>' + lang_obj.clue.list.no_match_trade.replace('%role%', ($('#Role').val() == 0 ? '<a href="/buyer/">' + lang_obj.header.buyer + '</a>' : '<a href="/supplier/">' + lang_obj.header.supplier + '</a>')) + '</span>';
                        new_html += '</div>';
                        new_html += '<div class="consultant_box">';
                        new_html += '<div class="c_box_content">';
                        new_html += '<div class="c_box_title">' + lang_obj.clue.list.consultant_title + '<span>' + lang_obj.clue.list.consultant + '</span></div>';
                        new_html += '<div class="c_box_desc">' + lang_obj.clue.list.consultant_desc + '</div>';
                        new_html += '<div class="c_box_phone line">' + lang_obj.clue.list.consultant_phone + ':<span>' + consultant.mobile + '</span></div>';
                        new_html += '<div class="c_box_email line">' + lang_obj.clue.list.consultant_qq + ':<span>' + consultant.qq + '</span></div>';
                        new_html += '</div>';
                        new_html += '<div class="c_box_qrcode">';
                        new_html += '<div class="qrcode"><img src="' + consultant.qrcode + '" alt="' + consultant.name + '"></div>';
                        new_html += '<div class="c_q_desc">' + lang_obj.clue.list.qrcode_desc + '</div>';
                        new_html += '</div>';
                        new_html += '</div>';
                        new_html += '</div>';
                        $('.data_main .bill .bottom .w').html(new_html);
                    }
                    else
                    {
                        $('.data_main .bill .bottom .table tbody').html(new_html);
                    }

                    $('.data_main .bill .bottom .load_out').hide();
                    if (zuoooo_config.Permit.limits['trade_list'] > 0)
                    {
                        $('#turn_page').addClass('level');
                    }

                    $('#turn_page').html(ret.page);
                    if (zuoooo_config.Permit.ability.indexOf('trade_list') == -1 || (false && zuoooo_config.Permit.limits['trade_list'] > 0))
                    {
                        let noViewHtml = themes_obj.noViewBox($('#AccountId').val(), 'ovtrade', this_size, lang_obj.global.trade);
                        $('.box_out .bill .bottom').addClass('no_view').append(noViewHtml);
                        let bottom_len = $(".box_out .bill .bottom.no_view").outerHeight();
                        if (bottom_len > $(".no_view_tip_box").outerHeight())
                        {
                            $('.no_view_tip_box').css({
                                top: '60px',
                                bottom: 'auto'
                            }).css('transform', 'translate(-50%,0%)');
                            $(".detail_out .box_out .bottom.no_view").attr('style', 'height:' + ($('.no_view_tip_box').outerHeight() + 120) + 'px;overflow: hidden;')
                        }
                    }
                    else if (zuoooo_config.Permit.limits['trade_detail'] < this_size && zuoooo_config.Permit.limits['trade_detail'])
                    {
                        let noViewHtml = themes_obj.noViewBox($('#AccountId').val(), 'ovtrade', ret.total, lang_obj.global.trade);
                        let bottom_len = 0;
                        $('.box_out .bill .bottom table .no_view').each(function()
                        {
                            bottom_len += $(this).outerHeight(true)
                        });
                        $('.box_out .bill .bottom').css('position', 'relative').append(noViewHtml);
                        let no_view_len = $(".no_view_tip_box").outerHeight();
                        if ((bottom_len - no_view_len - 120) > 0)
                        {
                            $('.no_view_tip_box').css({
                                top: 'auto',
                                bottom: (bottom_len - no_view_len - 60) + 'px'
                            }).css('transform', 'translate(-50%,0%)')
                        }
                        else
                        {
                            $('.no_view_tip_box').css({
                                top: 'auto',
                                bottom: ((bottom_len - no_view_len) / 2) + 'px'
                            }).css('transform', 'translate(-50%,0%)')
                        }
                    }
                    if (zuoooo_config.Permit.ability.indexOf('trade_detail') == -1 && zuoooo_config.Permit.limits['trade_detail'] == 0)
                    {
                        $('#turn_page').on('click', 'li a', function()
                        {
                            if ($('#AccountId').val() == 0)
                            {
                                themes_obj.loginBox('ovtrade')
                            }
                            else
                            {
                                themes_obj.upgradeBox('ovtrade')
                            }
                            return false
                        });
                    }
                }
                else if (result.code === 0)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close(result.msg, 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    })
                }
                else
                {
                    if (reloadtimes < 2) {
                        trade_list_timer = setTimeout(function()
                        {
                            trade_list_fun(0, reloadtimes + 1)
                        }, 2000);
                    }
                }
            },
            error:function(result)
            {
                if (result.status === 500)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close('System error, please try again later', 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    })
                    return false;
                }
                if (result.status === 401)
                {
                    themes_obj.loginBox('index_type');
                    return false
                }
                else if (result.status  === 403)
                {
                    themes_obj.upgradeBox('index_type');
                    return false;
                }
            }
        });
    }
    $(document).ready(function () {
        trade_list_fun();

        $('.data_main .bill .bottom .table tbody').delegate('.show-customs-record-detail', 'click', function () {
            if (zuoooo_config.Permit.ability.indexOf('popup_trade_detail') == -1)
            {
                if ($('#AccountId').val() == 0)
                {
                    themes_obj.loginBox('ovtrade');
                    return;
                }
                themes_obj.upgradeBox('ovtrade');
                return;
            }
            // 查找对应海关记录
            let row = [];
            for (let i = 0; i < rows.length; i++) {
                if (rows[i].snow_flake_id === $(this).attr('lid')) {
                    row = rows[i];
                }
            }
            if (!row)
            {
                global_obj.div_mask();
                global_obj.win_alert_auto_close('没有找到相关信息', 'fail', 2000, undefined, 0, function()
                {
                    global_obj.div_mask(1);
                });
                return;
            }
            // 替换弹窗内容
            var box = $(".trade_alert_detail");
            box.find('.date_text').text(row.date_text);
            box.find('.hs_code').text(row.hs_code);
            box.find('.importer_url').attr('href', row['buyer_url']).text(row.buyer_name);
            box.find('.importer_country_name').text(row.importer_country_name);
            box.find('.importer_address').text(row.buyer.raw_address);
            box.find('.exporter_url').attr('href', row['supplier_url']).text(row.seller_name);
            box.find('.exporter_country_name').text(row.exporter_country_name);
            box.find('.exporter_address').text(row.seller.raw_address);
            box.find('.local_port').text(row.local_port);
            box.find('.foreign_port').text(row.foreign_port);
            box.find('.foreign_port').text(row.foreign_port);
            box.find('.quantity').text(row.quantity);
            box.find('.weight').text(row.weight);
            box.find('.cif_unit').text(row.cif_unit);
            box.find('.manifest_number').text(row.manifest_number);
            box.find('.product_desc').text(row.product_desc);

            $('.trade_alert_detail,.div_mask').show();
        });

        $('.trade_alert_detail .close,.div_mask').on("click",function(){
            $('.trade_alert_detail,.div_mask').hide();
        })
    });
    // 分页点击
    $('#turn_page,.alert_turn_page').on('click','a',function() {
        if (zuoooo_config.Permit.ability.indexOf('trade_detail') == -1)
        {
            if ($('#AccountId').val() == 0)
            {
                themes_obj.loginBox('ovtrade')
            }
            else
            {
                themes_obj.upgradeBox('ovtrade')
            }
            return false
        }
        let page = $(this).data('page');
        if ($(this).parents('.jump').length) {
            page = $('input[name="jump_page"]').val();
            if (page == '' || page == undefined || page == 'undefined' || isNaN(page) || page <= 0 || page > parseInt($('input[name="jump_page"]').attr('max'))) {
                page = 1;
            }
        }

        // 滚动到列表顶部
        $('html, body').animate({
            scrollTop: $('.data_main > .wide > .data_item >.bill').offset().top
        }, 500);

        $('#trade_form input[name="page"]').val(page);
        trade_list_fun(1);
    });
    // 表单提交
    $('#trade_form').submit(function()
    {
        // if (zuoooo_config.Permit.ability.indexOf('trade_detail') == -1)
        // {
        //     if ($('#AccountId').val() == 0)
        //     {
        //         themes_obj.loginBox('ovtrade')
        //     }
        //     else
        //     {
        //         themes_obj.upgradeBox('ovtrade')
        //     }
        //     return false;
        // }
        trade_list_fun(1);
        return false;
    });
    $('#trade_form').addClass('is_load');
    if (zuoooo_config.IsMobile)
    {
        global_obj.div_mask();
        $('.member_reminder_box .account_box .reminder_menu_box .no_b').text('请前往PC端查看!');
        $('.member_reminder_box .account_box .reminder_menu_box .tips').hide();
        $('.member_reminder_box .account_box .rows_box').hide();
        $('.member_reminder_box').show();
        $('.member_reminder_box .close,#div_mask').on('click', function()
        {
            window.location.href = '/';
        })
    }
    $('.normal_search_but a').on('click', function()
    {
        if (zuoooo_config.Permit.ability.indexOf('search_filter') == -1)
        {
            if ($('#AccountId').val() == 0)
            {
                themes_obj.loginBox('advanced_search');
                return false
            }
            else
            {
                themes_obj.upgradeBox('advanced_search');
                return false
            }
        }
        else
        {
            // global_obj.div_mask();
            $('.advanced_search_box').show()
        }
    });
    $('body').on('click', '.advanced_search_box .close', function()
    {
        global_obj.div_mask(1);
        $('.advanced_search_box').hide();
    });
    $('input[name=title_jump_page]').on('input', function()
    {
        var total_page = $('.box_page_content span').text();
        if (Number($(this).val()) > Number(total_page))
        {
            $(this).val(total_page)
        }
    }).on('keydown', function(event)
    {
        if (event.keyCode == 13)
        {
            titlePageJump()
        }
    });
    $('.box_page_left').on('click', function()
    {
        var page = Number($('input[name=title_jump_page]').val());
        if (page > 1)
        {
            page = --page;
            $('input[name=title_jump_page]').val(page)
        }
        else
        {
            $('input[name=title_jump_page]').val(1)
        }
        titlePageJump()
    });
    $('.box_page_right').on('click', function()
    {
        var page = Number($('input[name=title_jump_page]').val());
        var total_page_num = Number($('.box_page_content span').text());
        if (page < total_page_num)
        {
            page = ++page;
            $('input[name=title_jump_page]').val(page)
        }
        else
        {
            $('input[name=title_jump_page]').val(total_page_num)
        }
        titlePageJump()
    });
    function titlePageJump()
    {
        if (zuoooo_config.Permit.ability.indexOf('trade_list') == -1)
        {
            if ($('#AccountId').val() == 0)
            {
                themes_obj.loginBox('trade_list')
            }
            else
            {
                themes_obj.upgradeBox('trade_list')
            }
            return false
        }
        var page = $('input[name=title_jump_page]').val();
        var url = '/trade/' + page + '.html?';
        var search = $('#search').val();
        if (search)
        {
            url += '&Search=' + search
        }
        window.location.href = url
    }
    $('input').each(function()
    {
        $(this).on('input', '', function()
        {
            if ($(this).attr('class') == 'keyword_input' || $(this).attr('class') == 'port_input')
            {
                $(this).parent('.input_box').css({
                    border: '2px solid #045afe',
                    padding: 0
                })
            }
            else
            {
                $(this).css({
                    border: '2px solid #045afe',
                    padding: '0px 0px 0px 10px'
                })
            }
            if ($(this).val().length == 0)
            {
                if ($(this).attr('class') == 'keyword_input' || $(this).attr('class') == 'port_input')
                {
                    $(this).parent('.input_box').css({
                        border: '1px solid #eeeeee',
                        padding: '1px'
                    })
                }
                else
                {
                    $(this).css({
                        border: '1px solid #eeeeee',
                        padding: '1px 1px 1px 10px'
                    })
                }
            }
        });
        $(this).on('change', '', function()
        {
            if ($(this).attr('class') == 'keyword_input' || $(this).attr('class') == 'port_input')
            {
                $(this).parent('.input_box').css({
                    border: '1px solid #eeeeee',
                    padding: '1px'
                })
            }
            else
            {
                $(this).css({
                    border: '1px solid #eeeeee',
                    padding: '1px 1px 1px 10px'
                })
            }
        })
    });
    $('.data_main .data_item .higer_box .name a').on("click",function(){
        if($(this).hasClass('cur')){
            $(this).removeClass('cur');
            $(this).html("展开 <img src=/assets/site_resource/image/user_more.png alt=>");
            $('.data_main .data_item .higer_box .record').css("display","none");
        }else{
            $(this).addClass('cur');
            $(this).html("收起 <img src=/assets/site_resource/image/user_more.png alt=>");
            $('.data_main .data_item .higer_box .record').css("display","flex");
        }
    });
    $('.change_replace').on('change', function()
    {
        var change_val = $(this).val();
        $('.advanced_search_box select[name=Role] option').each(function()
        {
            if ($(this).val() == change_val)
            {
                $(this).attr('selected', 'selected')
            }
            else
            {
                $(this).removeAttr('selected')
            }
        })
    });
    $('.change_keywords ul li').on('click', function()
    {
        if ($(this).attr('data-change') == 2)
        {
            if (zuoooo_config.Permit.ability.indexOf('hs_filter') == -1)
            {
                if ($('#AccountId').val() == 0)
                {
                    themes_obj.loginBox('tradesearch')
                }
                else
                {
                    themes_obj.upgradeBox('tradesearch')
                }
                return false
            }
        }
        $(this).addClass('current').siblings().removeClass('current');
        var change_title = $(this).attr('title');
        $('.advanced_search_box .input_box_content ul li').each(function()
        {
            if ($(this).attr('title') == change_title)
            {
                $(this).addClass('current')
            }
            else
            {
                $(this).removeClass('current')
            }
        });

        let _thisChange = parseInt($(this).data('change')),
            _thisParentDataName = $(this).parents('.fiter_change').data('name');
        $('#trade_form').find(`input[name=${_thisParentDataName}]`).val(_thisChange);

        $(this).parents('.change_keywords').find('.keyword_input').val('').attr({
            placeholder: $(this).attr('data-html'),
            name: $(this).attr('title')
        });
    });
    $('.change_keywords .keyword_input').on('input', function()
    {
        $('.advanced_search_box .keyword_box .keyword_input').val($(this).val())
    });
    $('.advanced_search_box select[name=Role]').on('change', function()
    {
        var change_val = $(this).val();
        $('.change_replace option').each(function()
        {
            if ($(this).val() == change_val)
            {
                $(this).attr('selected', 'selected')
            }
            else
            {
                $(this).removeAttr('selected')
            }
        })
    });
    $('.advanced_search_box .input_box_content ul li').on('click', function()
    {
        $(this).addClass('current').siblings().removeClass('current');
        var change_title = $(this).attr('title');
        $('.change_keywords ul li').each(function()
        {
            if ($(this).attr('title') == change_title)
            {
                $(this).addClass('current')
            }
            else
            {
                $(this).removeClass('current')
            }
        });
        $('.keyword_input').val('').attr({
            placeholder: $(this).attr('data-html'),
            name: $(this).attr('title')
        })
    });
    $('.advanced_search_box .keyword_box .keyword_input').on('input', function()
    {
        $('.change_keywords .keyword_input').val($(this).val())
    });
    if ($('input[name=Times]').length)
    {
        let startTimeDate = $('#startTimeDate').val();
        let endTimeDate = $('#endTimeDate').val();
        let selectTimeData = $('#selectTimeData').val();
        $('input[name=Times]').daterangepicker({
            format: "YYYY-MM-DD",
            separator: `   ${lang_obj.clue.detail.to}   `,
            showDropdowns: true,
            timePicker: false,
            startDate: selectTimeData,
            endDate: endTimeDate,
            minDate: startTimeDate,
            maxDate: endTimeDate
        })
    }
    $('.global_alert_box .close,.global_alert_box .cancel').on('click', function()
    {
        global_obj.div_mask(1);
        $('.global_alert_box').removeClass('cur')
    });
    $('.detail_out .box_out .level_search #trade_form .label input').focus(function(){});
    $('#trade_form').on('click', '.advanced_search_box button[type="submit"]', function()
    {
        if (zuoooo_config.Permit.ability.indexOf('search_filter') == -1)
        {
            $('.advanced_search_box').hide();
            if ($('#AccountId').val() == 0)
            {
                themes_obj.memberReminderBox('tradesearch')
            }
            else
            {
                themes_obj.upgradeBox('tradesearch')
            }
            return false
        }
        else
        {
            $('#trade_form').submit()
        }
    });
    $('#trade_form').on('click', '.normal_search_but button[type="button"]', function()
    {
        $('#trade_form .label input').val('');
        $('#trade_form .r_btn .left .filter button').each(function()
        {
            if (!$(this).hasClass('role'))
            {
                $(this).text('').removeClass('show')
            }
        });
        $('#trade_form').submit()
    }).on('click', '.advanced_search_box button[type="button"]', function()
    {
        $('#trade_form .label input').val('');
        $('.advanced_search_box .input_box_content ul li[title=products]').click();
        $('#trade_form .label select').val(0)
    });
    let trade_detail_timer = '';
    function trade_detail_fun(lid = 0)
    {
        clearTimeout(trade_detail_timer);
    }
    $('.detail_out').on('click', '.table tbody td a.go', function()
    {
        if (zuoooo_config.Permit.ability.indexOf('trade_detail') == -1)
        {
            if ($('#AccountId').val() == 0)
            {
                themes_obj.loginBox('ovtrade')
            }
            else
            {
                themes_obj.upgradeBox('ovtrade')
            }
            return false
        }
        let this_id = $(this).attr('lid');
        trade_detail_fun(this_id);
        global_obj.div_mask();
        $('.global_alert_box .detail_alert .record').addClass('load_out').html('<div class="no_data"><div class="loading_gif"></div></div>');
        $('.trade_alert_detail').addClass('cur')
    });
    $('#trade_form').find('input[name=TradeCountry2]').on('click keyup', function(event)
    {
        event.stopPropagation();
        var flag = true;
        $('.input_position').hide();
        let value = $(this).val();
        let ul = $(this).next('.input_position');
        let length = 0;
        ul.find('li').each(function()
        {
            let text = $(this).text().toLowerCase();
            if (text.indexOf(value.toLowerCase()) == -1)
            {
                $(this).hide()
            }
            else
            {
                $(this).show();
                length++
            }
        });
        if (length > 0)
        {
            ul.show()
        }
        let fist_child;
        $("ul[name='importer_country']").find("li").each(function()
        {
            if ($(this).css('display') == 'list-item')
            {
                if (!fist_child)
                {
                    fist_child = $(this)
                }
            }
        });
        if (fist_child)
        {
            let li_country = fist_child.data('value');
            $("input[name='importer_country']").val(li_country);
        }
        else
        {
            $("input[name='importer_country']").val('');
        }
        $(document).bind("click", function(e)
        {
            var target = $(e.target);
            if (target.closest($('.input_position')).length == 0 && flag == true)
            {
                $('.input_position').hide();
                flag = false;
            }
        });
        if (value.length == 0)
        {
            $(this).parents('.input_box').find('input[name="importer_country"]').val('');
            $('#trade_form .r_btn .left .filter button[data-name=TradeCountry]').text('').removeClass('show');
            event.preventDefault();
            return false;
        }
    });
    $('#trade_form').find('input[name=Country2]').on('click keyup', function(event)
    {
        event.stopPropagation();
        var flag = true;
        $('.input_position').hide();
        let value = $(this).val();
        let ul = $(this).next('.input_position');
        let length = 0;
        ul.find('li').each(function()
        {
            let text = $(this).text().toLowerCase();
            if (text.indexOf(value.toLowerCase()) == -1)
            {
                $(this).hide()
            }
            else
            {
                $(this).show();
                length++
            }
        });
        if (length > 0)
        {
            ul.show();
            ul.addClass('input_position_p')
        }
        let fist_child;
        $("ul[name='exporter_country']").find("li").each(function()
        {
            if ($(this).css('display') == 'list-item')
            {
                if (!fist_child)
                {
                    fist_child = $(this);
                }
            }
        });
        if (fist_child)
        {
            let li_country = fist_child.data('value');
            $("input[name='exporter_country']").val(li_country);
        }
        else
        {
            $("input[name='exporter_country']").val('');
        }
        $(document).bind("click", function(e)
        {
            var target = $(e.target);
            if (target.closest($('.input_position')).length == 0 && flag == true)
            {
                $('.input_position').hide();
                flag = false;
            }
        });
        if (value.length == 0)
        {
            $(this).parents('.input_box').find('input[name="exporter_country"]').val('');
            $('#trade_form .r_btn .left .filter button[data-name=Country]').text('').removeClass('show');
            event.preventDefault();
            return false;
        }
    });
    $('#trade_form .input_position').on('click', 'li', function()
    {
        $(this).addClass('current').siblings().removeClass('current');
        $(this).parent().prev('input').val($(this).find('span').text());
        $(this).parents('.input_box').find('input[name="' + $(this).parent().attr('name') + '"]').val($(this).data('value'));
        if ($(this).parent().attr('name') == 'importer_country')
        {
            $('#trade_form .r_btn .left .filter button[data-name=importer_country]').text($(this).find('span').text()).addClass('show')
        }
        else
        {
            $('#trade_form .r_btn .left .filter button[data-name=exporter_country]').text($(this).find('span').text()).addClass('show')
        }
        $('#trade_form .input_position').hide();
    });
    $('#trade_form .r_btn').on('click', 'button.close', function()
    {
        let name = $(this).data('name');
        $('#trade_form input[name=' + name + ']').val('');
        $(this).removeClass('show');
        if (name == 'TradeCountry')
        {
            $('#trade_form input[name=TradeCountry2]').val('');
            $('.input_position').hide()
        }
        if (name == 'Country')
        {
            $('#trade_form input[name=Country2]').val('');
            $('.input_position').hide()
        }
        if (name == 'SupplierCountry')
        {
            $('#trade_form input[name=SupplierCountry2]').val('');
            $('.input_position').hide()
        }
        $('#trade_form').submit();
        return false
    });
    $('#trade_form').on('change', 'input', function()
    {
        if ($(this).attr('name') == 'TradeCountry2' || $(this).attr('name') == 'Country2' || $(this).attr('name') == 'Times' || $(this).attr('name') == 'SupplierCountry2')
        {
            return false
        }
        if ($(this).val())
        {
            $('#trade_form .r_btn .left .filter button[data-name=' + $(this).attr('name') + ']').text($(this).val()).addClass('show')
        }
        else
        {
            $('#trade_form .r_btn .left .filter button[data-name=' + $(this).attr('name') + ']').text('').removeClass('show')
        }
    });
    $('.daterangepicker').on('click', '.btn-success', function()
    {
        $('#trade_form .r_btn .filter button[data-name=Times]').text($('#trade_form input[name=Times]').val()).addClass('show')
    });
    $('body').on('click', '.turn_page_no_permit .close', function()
    {
        global_obj.div_mask(1);
        $('.turn_page_no_permit').hide()
    });
});
